<template>
  <el-row type="flex" justify="space-between" align="middle" style="height:40px; width:100%">
    <el-col :span="20"> <span>{{ nodeData.name }}</span></el-col>
    <el-col :span="4">
      <!-- 主体部-树状结构 -->
      <el-row type="flex">
        <el-col :span="12">{{ nodeData.manager }}</el-col>
        <el-col :span="12">
          <!-- 下拉菜单 -->
          <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item command="del">删除部门</el-dropdown-item>
              <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>

        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { reqDelDepartments } from '@/api/departments'
export default {
  name: 'TreeTools',
  props: {
    nodeData: {
      type: Object,
      required: true
    },
    departs: {
      type: Array,
      required: true
    }
  },
  methods: {
    // 注册点击事件【el-dropdown内部提供】
    handleCommand(command) {
      // ** 添加 弹框
      if (command === 'add') {
        // this.nodeData作为一级部门【父】
        // 添加部门要告诉父组件添加的是哪个部门
        this.$emit('addDepts', this.nodeData)
      }
      // ** 删除 弹框
      if (command === 'del') {
        // console.log(this.nodeData)
        this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
        }).then(async() => {
          await reqDelDepartments(this.nodeData.id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
        // 删除后刷新页面，重新发送请求，字传父[不需要传参，因为请求已经发送了]
        this.$emit('delDepartmentsItem')
      }
      // ** 编辑 弹框
      if (command === 'edit') {
        // this.nodeData是自己 编辑自己
        this.$emit('edit-depts', this.nodeData)
      }
    }
  }
}
</script>

<style>

</style>
